#include "qscieditor.h"
QsciEditor::QsciEditor(QWidget *parent) : QsciScintilla(parent)
{
    init();
}

void QsciEditor::init(){
    //显示行号
    this->setMarginType(0,QsciScintilla::NumberMargin);//设置编号为0的页边显示行号。
    this->setMarginLineNumbers(0,true);//对该页边启用行号
    this->setMarginWidth(0,50);//设置页边宽度

    //QsciLexerLua *textLexer = new QsciLexerLua;//创建一个词法分析器
    //lexer= new QsciLexerJava;
    //this->setLexer(lexer);//给QsciScintilla设置词法分析器

    //初始设置c++解析器
    //this->setLexer(new QsciLexerCPP(this));

    //设置光标所在行背景色
    QColor lineColor = QColor(Qt::darkGray).lighter(160);
    this->setCaretLineBackgroundColor(lineColor);
    this->setCaretLineVisible(true);
    this->setSelectionBackgroundColor(Qt::black);
    this->setSelectionForegroundColor(Qt::white);

    //设置括号等自动补全
    this->setAutoIndent(true);

    //得到光标位置
    //int line,index;
    //this->getCursorPosition(&line,&index);

    // 编码
    this->SendScintilla(QsciScintilla::SCI_SETCODEPAGE, QsciScintilla::SC_CP_UTF8);

    //自动缩进
    this->setAutoIndent(false);
    // 括号
    this->setUnmatchedBraceBackgroundColor(Qt::blue);
    this->setBraceMatching(QsciScintilla::SloppyBraceMatch);

    this->setScrollWidth(5);
    this->setScrollWidthTracking(true);

    //设置光标颜色
    QColor col(Qt::blue);
    this->setCaretForegroundColor(col);
    //设置光标宽度
    this->setCaretWidth(20);

    // 设置Tab键为4个空格
    this->setTabWidth(4);

    //换行符
    this->setEolMode(QsciScintilla::EolUnix);
}

void QsciEditor::setLexerFont(QFont f)
{
    if (lexer != nullptr) {
        lexer->setFont(f);
    }
}

void QsciEditor::setAllFonts(QFont f)
{
    if (lexer != nullptr) {
        lexer->setFont(f);
    }
    this->setFont(f);
    this->setLexerFont(f);
    this->currentFont=f;
}

QFont QsciEditor::getCurrentFont()
{
    return currentFont;
}

void QsciEditor::updateLexer(QString fileName, QString suffix, bool isDarkTheme)
{
    delete lexer;
    lexer = nullptr;

    suffix = suffix.toUpper();
    if (suffix == "AVS") { lexer = new QsciLexerAVS; }
    else if (suffix == "SH") { lexer = new QsciLexerBash; }
    else if (suffix == "BATCH") { lexer = new QsciLexerBatch; }
    else if (fileName == "CMakeLists.txt") { lexer = new QsciLexerCMake; }
    else if (suffix == "COFFEESCRIPT") { lexer = new QsciLexerCoffeeScript; }
    else if (suffix == "CPP") { lexer = new QsciLexerCPP; }
    else if (suffix == "CS") { lexer = new QsciLexerCSharp; }
    else if (suffix == "CSS") { lexer = new QsciLexerCSS; }
    else if (suffix == "D") { lexer = new QsciLexerD; }
    else if (suffix == "DIFF") { lexer = new QsciLexerDiff; }
    else if (suffix == "EDIFACT") { lexer = new QsciLexerEDIFACT; }
    else if (suffix == "FOR") { lexer = new QsciLexerFortran; }
    else if (suffix == "FORTRAN77") { lexer = new QsciLexerFortran77; }
    else if (suffix == "HTML") { lexer = new QsciLexerHTML; }
    else if (suffix == "IDL") { lexer = new QsciLexerIDL; }
    else if (suffix == "JAVA") { lexer = new QsciLexerJava; }
    else if (suffix == "JS") { lexer = new QsciLexerJavaScript; }
    else if (suffix == "JSON") { lexer = new QsciLexerJSON; }
    else if (suffix == "LUA") { lexer = new QsciLexerLua; }
    else if (fileName == "Makefile") { lexer = new QsciLexerMakefile; }
    else if (suffix == "MARKDOWN"||suffix == "MD") { lexer = new QsciLexerMarkdown; }
    else if (suffix == "MATLAB") { lexer = new QsciLexerMatlab; }
    else if (suffix == "OCTAVE") { lexer = new QsciLexerOctave; }
    else if (suffix == "PAS") { lexer = new QsciLexerPascal; }
    else if (suffix == "PL") { lexer = new QsciLexerPerl; }
    else if (suffix == "PO") { lexer = new QsciLexerPO; }
    else if (suffix == "PS"||suffix == "EPS") { lexer = new QsciLexerPostScript; }
    else if (suffix == "POV") { lexer = new QsciLexerPOV; }
    else if (suffix == "PROPERTIES") { lexer = new QsciLexerProperties; }
    else if (suffix == "PY") { lexer = new QsciLexerPython; }
    else if (suffix == "RB") { lexer = new QsciLexerRuby; }
    else if (suffix == "SPICE") { lexer = new QsciLexerSpice; }
    else if (suffix == "SQL") { lexer = new QsciLexerSQL; }
    else if (suffix == "TCL") { lexer = new QsciLexerTCL; }
    else if (suffix == "TEX") { lexer = new QsciLexerTeX; }
    else if (suffix == "VERILOG") { lexer = new QsciLexerVerilog; }
    else if (suffix == "VHDL") { lexer = new QsciLexerVHDL; }
    else if (suffix == "XML") { lexer = new QsciLexerXML; }
    else if (suffix == "YAML"||suffix == "YML") { lexer = new QsciLexerYAML; }
    else {}

    if (lexer == nullptr) {
        this->setLexer(nullptr);
        this->setFont(currentFont);
    }else {
        if (isDarkTheme) {
            // 通过 Lexer 配置语法高亮（仅适用于支持语法高亮的 lexer）
            //lexer->setPaper(QColor("#000000"));  // 黑色背景
            //lexer->setColor(QColor("#FFFFFF")); // 白色字体
            lexer->setDefaultPaper(QColor("#1e1f22"));// 默认背景颜色
            lexer->setDefaultColor(QColor("#FFFFFF"));// 默认字体颜色
        }
        this->setLexer(lexer);
        lexer->setFont(currentFont);
    }
}

//更新行号宽度
void QsciEditor::updateLineNumberWidth()
{
    int lines = this->lines();
    if(curLines==lines) return;
    curLines=lines;
    QString lineNumber = QString::number(lines);
    int width = this->fontMetrics().width(lineNumber)+20;
    if(marginWidth==width) return;
    marginWidth=width;
    this->setMarginWidth(0, width);
    //qDebug() << "setMarginWidth width=" << width << endl;
}

// 重写焦点获得事件
void QsciEditor::focusInEvent(QFocusEvent *event)
{
    // 确保当前行背景高亮显示
//    SendScintilla(SCI_SETCARETLINEBACK, QColor("#2d2d2d").rgba());
//    this->setSelectionBackgroundColor(QColor("#2d2d2d").rgba());
//    SendScintilla(SCI_SETCARETLINEVISIBLE, 1);

    // 确保光标颜色
//    SendScintilla(SCI_SETCARETFORE, QColor("#FFFFFF").rgba());
//    this->setCaretForegroundColor(QColor("#FFFFFF").rgba());


    qDebug() << "Editor focus in";
    QsciScintilla::focusInEvent(event); // 调用父类方法
}

// 重写焦点失去事件
void QsciEditor::focusOutEvent(QFocusEvent *event)
{
    // 保持当前行背景高亮显示
//    SendScintilla(SCI_SETCARETLINEBACK, QColor("#2d2d2d").rgba());
//    this->setSelectionBackgroundColor(QColor("#2d2d2d").rgba());
//    SendScintilla(SCI_SETCARETLINEVISIBLE, 1);

    // 保持光标颜色
//    SendScintilla(SCI_SETCARETFORE, QColor("#FFFFFF").rgba());
//    this->setCaretForegroundColor(QColor("#FFFFFF").rgba());

    qDebug() << "Editor focus out";
    QsciScintilla::focusOutEvent(event); // 调用父类方法
}
